package br.ufrj.xml.model.persistence.dao.jdbc;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.List;

import br.ufrj.xml.model.persistence.dao.ColumnDAO;
import br.ufrj.xml.model.persistence.entity.ColumnVO;
import br.ufrj.xml.model.persistence.entity.TableVO;

public class ColumnJdbcDAO extends GenericJdbcDAO<ColumnVO>  implements ColumnDAO {

	private static final String BASIC_QUERY = "select pg_attribute.attname from pg_attribute";
	
	public ColumnJdbcDAO(Connection connection) {
		super(connection);
		// TODO Auto-generated constructor stub
	}

	public List<ColumnVO> findByNameTable(String nmTable) {
		String sql = "select pg_attribute.attname, pg_type.typname, pg_attribute.attnotnull " +
					 "from pg_attribute, pg_class , pg_type " +
					 "where pg_attribute.attnum > 0 " +
					 "and pg_attribute.attrelid = pg_class.oid " +
					 "and pg_type.oid = pg_attribute.atttypid " +
					 "and upper(pg_class.relname) = ? ";
		
		List<ColumnVO> list = query(sql, 
							new Object[]{ nmTable.toUpperCase() },
							new int[]{ Types.VARCHAR });
		return list;
	}

	public int count() {
		// TODO Auto-generated method stub
		return 0;
	}

	public void create(ColumnVO entity) {
		// TODO Auto-generated method stub

	}

	public void delete(Integer id) {
		// TODO Auto-generated method stub

	}

	public List<ColumnVO> findAll() {
		// TODO Auto-generated method stub
		return null;
	}

	public List<ColumnVO> findPageByPage(int firstResult, int maxResults) {
		// TODO Auto-generated method stub
		return null;
	}

	public ColumnVO read(Integer id) {
		// TODO Auto-generated method stub
		return null;
	}

	public void update(ColumnVO entity) {
		// TODO Auto-generated method stub

	}

	public ColumnVO mapRow(ResultSet result) throws SQLException {
		int i = 1;
		ColumnVO columnVO = new ColumnVO();
		columnVO.setName(result.getString(i++));
		columnVO.setType(result.getObject(i++).toString());
		columnVO.setNotNull(result.getBoolean(i++));
		
		if (columnVO.getName().contains("parentid")){
			columnVO.setForeignKey(true);
		}
		else if(columnVO.getName().contains("pkid")){
			columnVO.setPrimaryKey(true);
		}
		
		return columnVO;
	}

}
